//
//  LCZMyselfViewController.m
//  PersonalNews
//
//  Created by Golibyo on 15/11/26.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZMyselfViewController.h"
#import "LCZMyselfCell.h"
#import "LCZBaseCollectionView.h"
#import "LCZHistoryViewController.h"
#import <MessageUI/MessageUI.h>
#import "CacheOrClearCache.h"
#import "SendEmail.h"
#import "LCZSaveController.h"
#import "LCZIsNight.h"

@interface LCZMyselfViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, MFMailComposeViewControllerDelegate,UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)LCZBaseImageView *backImageView;
@property (nonatomic, strong)LCZBaseCollectionView *myCollectionView;
@property (nonatomic, strong)LCZBaseView *settingView;
@property (nonatomic, strong)NSMutableArray *setArr;
@property (nonatomic, strong)LCZBaseLabel *ittitudeLabel;
@property (nonatomic, strong)LCZBaseLabel *newsIttiLabel;
@property (nonatomic, strong)LCZBaseView *cangView;
@property (nonatomic, strong)LCZBaseImageView *touxiangImageView;

@property (nonatomic, strong)LCZBaseTableView *tableView;
@end

@implementation LCZMyselfViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:1 green:0.91 blue:0.91 alpha:1];
    // Do any additional setup after loading the view.
    
    self.cangView = [[LCZBaseView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:self.cangView];
   
    self.setArr = [NSMutableArray arrayWithObjects:@"   清除缓存   ", @"   关于我们   ", @"   夜间模式     ", @"  和我们聊聊  ", @"    版本号    ", nil];
        
    self.navigationController.navigationBarHidden = YES;
    
    // 背景图片
    self.backImageView = [[LCZBaseImageView alloc]init];
    [self.cangView addSubview:self.backImageView];
    self.backImageView.image = [UIImage imageNamed:@"back.png"];
    
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(self.view);
        make.size.mas_equalTo(self.view.frame.size);
        
    }];
    
    // 头像
    self.touxiangImageView = [[LCZBaseImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 150) / 2, self.view.frame.size.height / 3, 150, 150)];
    [self.cangView addSubview:self.touxiangImageView];
    self.touxiangImageView.image = [UIImage imageNamed:@"touxiang.png"];
    
    // 态度label
    self.ittitudeLabel = [[LCZBaseLabel alloc]init];
    [self.cangView addSubview:self.ittitudeLabel];
    self.ittitudeLabel.text = [NSString stringWithFormat:@"这个世界疯狂, 没人性, 腐败. 您却一直清醒, 温柔, 一尘不染"];
    self.ittitudeLabel.textAlignment = NSTextAlignmentCenter;
    self.ittitudeLabel.numberOfLines = 0;
    self.ittitudeLabel.font = [UIFont systemFontOfSize:14];
    self.ittitudeLabel.frame = CGRectMake(55, self.view.frame.size.height - self.view.frame.size.width / 5 - 150, self.view.frame.size.width - 110, 50);

    self.newsIttiLabel = [[LCZBaseLabel alloc]init];
    [self.cangView addSubview:self.newsIttiLabel];
    self.newsIttiLabel.text = [NSString stringWithFormat:@"做新闻有态度"];
    self.newsIttiLabel.textAlignment = NSTextAlignmentCenter;
    self.newsIttiLabel.frame = CGRectMake(100, self.view.frame.size.height - self.view.frame.size.width / 5 - 100, self.view.frame.size.width - 200, 100);

    // 最上面的返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(15, 30, 30, 30);
    backButton.layer.cornerRadius = 15;
    
    backButton.backgroundColor = [UIColor colorWithRed:0.45 green:0.46 blue:0.46 alpha:1];
    [self.view addSubview:backButton];
    [backButton setImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];
    [backButton setTintColor:[UIColor whiteColor]];
    [backButton addTarget:self action:@selector(presentLeftMenuViewController:) forControlEvents:UIControlEventTouchUpInside];

    // collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 3, self.view.frame.size.width / 6);
    
    
    self.myCollectionView = [[LCZBaseCollectionView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - self.view.frame.size.width / 6, self.view.frame.size.width, self.view.frame.size.width / 6) collectionViewLayout:flowLayout];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.cangView addSubview:self.myCollectionView];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;

    
    [self.myCollectionView registerClass:[LCZMyselfCell class] forCellWithReuseIdentifier:@"reuse"];
    
}

#pragma mark 设置的点击方法
- (void)setting
{
    self.cangView.hidden = YES;
    
    self.settingView = [[LCZBaseView alloc]init];
    self.settingView.frame = CGRectMake(40, self.view.frame.size.height, self.view.frame.size.width - 80, 360);
    
    [LCZBaseView animateWithDuration:0.5 animations:^{
       
        self.settingView.layer.cornerRadius = 5;
        self.settingView.layer.masksToBounds = YES;
        
        self.settingView.backgroundColor = [UIColor colorWithRed:0.22 green:0.22 blue:0.22 alpha:1];
        [self.view addSubview:self.settingView];
        self.settingView.frame = CGRectMake(40, self.view.frame.size.height / 2 - 200, self.view.frame.size.width - 80, 360);
        // 顶图片
        LCZBaseImageView *lvImageView = [[LCZBaseImageView alloc]initWithFrame:CGRectMake(15, 15, 30, 30)];
        [self.settingView addSubview:lvImageView];
        lvImageView.image = [UIImage imageNamed:@"lv.png"];
        // 返回按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake((self.view.frame.size.width - 80 - 50) / 2, 360 - 50, 50, 50);
        [self.settingView addSubview:btn];
        btn.backgroundColor = [UIColor whiteColor];
        [btn setImage:[UIImage imageNamed:@"xiaoCuo.png"] forState:UIControlStateNormal];
        [btn setTintColor:[UIColor grayColor]];
        btn.layer.cornerRadius = 50;
        btn.layer.masksToBounds = YES;
        [btn addTarget:self action:@selector(fanhui:) forControlEvents:UIControlEventTouchUpInside];
        
        [self create];
    }];
}

// 弹出页的返回按钮
- (void)fanhui:(UIButton *)button
{
    self.cangView.hidden = NO;
    
    [LCZBaseView animateWithDuration:0.5 animations:^{
        self.settingView.frame = CGRectMake(40, self.view.frame.size.height, self.view.frame.size.width - 80, 360);
        
    }];
}
#pragma mark 创建系统按钮上的功能按钮
- (void)create
{
    self.tableView = [[LCZBaseTableView alloc]initWithFrame:CGRectMake(0, 60, self.settingView.frame.size.width, self.settingView.frame.size.height - 110) style:UITableViewStylePlain];
    [self.settingView addSubview:self.tableView];
    self.tableView.delegate =self;
    self.tableView.dataSource =self;
    self.tableView.backgroundColor = [UIColor colorWithRed:0.16 green:0.15 blue:0.15 alpha:1];
    self.tableView.rowHeight = 50;
    
    
}
#pragma mark tableView的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    LCZBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[LCZBaseTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        cell.backgroundColor = [UIColor colorWithRed:0.16 green:0.15 blue:0.15 alpha:1];
        cell.textLabel.text = self.setArr[indexPath.row];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.imageView.image = [UIImage imageNamed:@"kongxin.png"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            [self qing];
            break;
        case 1:
            [self guan];
            break;
        case 2:
            [self ye];
            break;
        case 3:
            [self he];
            break;
        case 4:
            [self tong];
            break;
    }
}

#pragma mark collectionView的协议方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LCZMyselfCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    NSArray *arr = [NSArray arrayWithObjects:@"shoucang.png",@"lishi.png", @"xitong.png", nil];
    NSArray *ziArr = [NSArray arrayWithObjects:@"收藏",@"历史",@"设置", nil];
    cell.myImage.image = [UIImage imageNamed:arr[indexPath.item]];
    cell.myLabel.text = ziArr[indexPath.item];
    cell.backgroundColor = [UIColor colorWithRed:0.64 green:0.93 blue:0.85 alpha:1];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.item) {
        case 0:{
            LCZSaveController *saveVC = [[LCZSaveController alloc] init];
            [self.navigationController pushViewController:saveVC animated:YES];
        }
            break;
        case 1:{
            LCZHistoryViewController *historyVC = [[LCZHistoryViewController alloc] init];
            [self.navigationController pushViewController:historyVC animated:YES];
        }
            break;
        case 2:
            [self setting];
            break;
    }
}

- (void)qing
{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *myCache = [NSString stringWithFormat:@"%.2fM", [CacheOrClearCache folderSizeAtPath:sandBoxPath]];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清除缓存" message:myCache preferredStyle:UIAlertControllerStyleAlert];
    //清除缓存
    UIAlertAction *clearAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [CacheOrClearCache clearCache];
    }];
    [alert addAction:clearAction];
    [self presentViewController:alert animated:YES completion:^{
    }];
}

// 关于我们
- (void)guan
{
    
}
// 夜间模式
- (void)ye
{
    
    
    if ([DKNightVersionManager currentThemeVersion] == DKThemeVersionNormal) {
        [DKNightVersionManager nightFalling];
        NSLog(@"夜间模式");
    } else {
        [DKNightVersionManager dawnComing];
        NSLog(@"日间模式");
    }
    
    
}

// button的点击事件 发送邮件
- (void)he
{
    
    Class mailClass = NSClassFromString(@"MFMailComposeViewController");
    if (mailClass) {
        
        if ([mailClass canSendMail]) {
            MFMailComposeViewController *mailBox = [[MFMailComposeViewController alloc]init];
            [SendEmail displayMailBox:mailBox];
            // 设置代理人
            mailBox.mailComposeDelegate = self;
            [self presentViewController:mailBox animated:YES completion:^{                
            }];
        }
        else {
            [SendEmail showMailBoxOnDevice];
        }
    }else {
        [SendEmail showMailBoxOnDevice];
    }
}

//// 打开设备自带的邮箱工具
- (void)showMailBoxOnDevice{
    NSString *mail = @"971846558@qq.com";
    mail = [mail stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:mail]];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mail]];
}

- (void)displayMailBox {
    // 创建邮件视图VC
    MFMailComposeViewController *mailBox = [[MFMailComposeViewController alloc] init];
    // 填写收件人地址
    NSArray *recipient = [NSArray arrayWithObject:@"971846558@qq.com"];
    [mailBox setToRecipients:recipient];
    // 填写主题
    [mailBox setSubject:@"意见反馈"];
    // 填写邮件内容
    NSString *messageBody = @"134335";
    [mailBox setMessageBody:messageBody isHTML:YES];
    // 设置代理人
    mailBox.mailComposeDelegate = self;
    [self presentViewController:mailBox animated:YES completion:^{
        
    }];    
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    NSString *message;
    switch (result) {
        case MFMailComposeResultCancelled:
            message = @"取消发送";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultSaved:
            message = @"保存草稿";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultSent:
            message = @"已发送";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultFailed:
            message = @"发送失败";
            NSLog(@"%@", message);
            break;
    }
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)tong
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"版本号" message:@"1.0" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
